package cn.smile.controller.core;

import cn.smile.bean.dto.core.IpQueryDTO;
import cn.smile.business.core.IReqWithoutService;
import cn.smile.commons.constant.WithoutUrlConstant;
import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpMethod;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import java.util.HashMap;

/**
 * 用于Shell获取外网IP修改DDNS使用
 */
@Slf4j
@RestController
@RequestMapping("api/ip")
public class IpController {

    @Resource
    private IReqWithoutService outService;

    @GetMapping("getServerIp")
    public String getServerIp() {
        log.info("[IpController].[getServerIp] ------> 查询服务器IP地址 Start");
        String ipResult = outService.reqWithOutUrl(WithoutUrlConstant.QUERY_IP_URL, new HashMap<>(), HttpMethod.GET, Boolean.TRUE);
        String ip;
        if (ObjectUtils.isEmpty(ipResult)) {
            ip = null;
        } else {
            IpQueryDTO dto = JSON.parseObject(ipResult, IpQueryDTO.class);
            log.info("dto = {}", JSON.toJSONString(dto));
            ip = dto.getIp();
        }
        log.info("[IpController].[getServerIp] ------> 查询服务器IP地址 End, ip = {}", ip);
        return ip;
    }
}
